package com.suns.server;

import com.suns.HelloServiceGrpc;
import com.suns.SimpleRequest;
import com.suns.SimpleResponse;
import io.grpc.stub.StreamObserver;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author xiangminwen
 * @date 2024/4/2 17:09
 */
public class HelloServiceImpl extends HelloServiceGrpc.HelloServiceImplBase {

    /**
     * 1.接受client提交的参数
     * 2.业务处理调用对应业务功能
     * 3.提供返回值
     */
    @Override
    public void hello(SimpleRequest request, StreamObserver<SimpleResponse> response) {
        System.out.println("HelloService hello:" + request.getName() + ":" + request.getAge() + "codes:" + Arrays.toString(request.getCodeList().toArray()));
        SimpleResponse.Builder builder = SimpleResponse.newBuilder();
        SimpleResponse res = builder.setCode(0).setData("hello:" + request.getName()).build();
        //设置返回对象
        response.onNext(res);
        //通知客户端服务结束
        response.onCompleted();
    }

    @Override
    public void hello1(SimpleRequest request, StreamObserver<SimpleResponse> response) {
        System.out.println("HelloService hello1:" + request.getName() + ":" + request.getAge() + "codes:" + Arrays.toString(request.getCodeList().toArray()));
        for (int i = 0; i < 3; i++) {
            SimpleResponse.Builder builder = SimpleResponse.newBuilder();
            response.onNext(builder.setCode(0).setData("hello:" + request.getName() + ":" + i).build());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException ignore) {
            }
        }
        //通知客户端服务结束
        response.onCompleted();
    }

    /**
     * 监控客户端请求
     * 监控异常
     * 监控所有消息发送完成
     */
    @Override
    public StreamObserver<SimpleRequest> hello2(StreamObserver<SimpleResponse> response) {
        return new StreamObserver<SimpleRequest>() {

            @Override
            public void onNext(SimpleRequest value) {
                System.out.println("HelloService hello2:" + value.getName() + ":" + value.getAge() + "codes:" + Arrays.toString(value.getCodeList().toArray()));
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onCompleted() {
                System.out.println("HelloService hello2 onCompleted");
                response.onNext(SimpleResponse.newBuilder().setCode(0).setData("HelloService hello2 onCompleted").build());
                response.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<SimpleRequest> hello3(StreamObserver<SimpleResponse> response) {
        return new StreamObserver<SimpleRequest>() {

            @Override
            public void onNext(SimpleRequest value) {
                System.out.println("HelloService hello2:" + value.getName() + ":" + value.getAge() + "codes:" + Arrays.toString(value.getCodeList().toArray()));
                response.onNext(SimpleResponse.newBuilder().setCode(0).setData("hello3:" + value.getName()).build());
            }

            @Override
            public void onError(Throwable t) {
            }

            @Override
            public void onCompleted() {
                System.out.println("HelloService hello2 onCompleted");
                response.onCompleted();
            }
        };
    }
}
